package com.machine.filling.db.dao;

import android.provider.Settings;

import com.machine.filling.MyApp;
import com.machine.filling.db.greendao.GunInfoModleDao;
import com.machine.filling.db.modle.GunInfoModle;
import com.machine.filling.net.ws.response.WsResTopGunsInfoBean;

import java.util.List;

/**
 * Created by Administrator on 2018/3/10 0010.
 */

public class GunInfoDao {

    private static GunInfoDao instance;
    private static GunInfoModleDao gunInfoModleDao;
    private static GunInfoModle gunInfoModle;
    private final String deviceNum;

    private GunInfoDao() {
        deviceNum = Settings.System.getString(MyApp.getInstance().getContentResolver(), Settings.Secure.ANDROID_ID);

    }

    public static GunInfoDao getInstance() {
        if (instance == null) {
            instance = new GunInfoDao();
            gunInfoModleDao = MyApp.getInstance().getDaoSession().getGunInfoModleDao();
            gunInfoModle = new GunInfoModle();
        }
        return instance;
    }

    public void addGun(GunInfoModle gunInfoModle) {
        GunInfoModle gun = gunInfoModleDao.queryBuilder().where(GunInfoModleDao.Properties.GunId.eq(gunInfoModle.getGunId())).unique();
        if (gun == null){
            gunInfoModleDao.insert(gunInfoModle);
        }else {
            //// TODO: 2018/4/16 0016 此处出错了
            gunInfoModle.setAutoId(gun.getAutoId());
            gunInfoModleDao.update(gunInfoModle);
        }
    }

    public List<GunInfoModle> queryAll() {
        return gunInfoModleDao.queryBuilder().list();

    }

    /**
     * 通过guiid 查找gun
     * @param gunId
     * @return
     */
    public GunInfoModle queryByGunId(String gunId) {
        List<GunInfoModle> list = gunInfoModleDao.queryBuilder().where(GunInfoModleDao.Properties.GunId.eq(gunId)).list();
        return (list==null || list.size() == 0) ? null : list.get(list.size() - 1);
    }

    /**
     * 通过串口号  查找gun
     * @param portNum
     * @return
     */
    public GunInfoModle queryBytPortNum(String portNum){
        List<GunInfoModle> list = gunInfoModleDao.queryBuilder().where(GunInfoModleDao.Properties.PortNum.eq(portNum)).list();
        return (list==null || list.size() == 0) ? null : list.get(list.size() - 1);
    }

    /**
     * 根据首页http请求获取到的信息,刷新油枪信息
     * @param wsBean
     */
    public void updateGunInfoFromTop(WsResTopGunsInfoBean.OilGunOutputBean wsBean) {
        GunInfoModle gunInfoModle = queryByGunId(""+wsBean.getGunId());
        if(gunInfoModle == null){
            gunInfoModle = new GunInfoModle();
            gunInfoModle.setGunId(wsBean.getGunId());
            gunInfoModle.setGunName(wsBean.getGunName());
            gunInfoModle.setGunNum(wsBean.getGunNum());
            gunInfoModle.setPortNum(wsBean.getPortNum());
            gunInfoModle.setPrice(wsBean.getPrice());
            gunInfoModle.setTypeName(wsBean.getTypeName());
            gunInfoModle.setTypeShortName(wsBean.getTypeName());
            gunInfoModle.setGunId(wsBean.getGunId());
            gunInfoModle.setTypeId(wsBean.getTypeId());
            gunInfoModle.setCuPrice(wsBean.getCuPrice());
            gunInfoModleDao.insert(gunInfoModle);
        }
        gunInfoModle.setGunName(wsBean.getGunName());
        gunInfoModle.setPortNum(wsBean.getPortNum());
        gunInfoModle.setPrice(wsBean.getPrice());
        gunInfoModle.setGunNum(wsBean.getGunNum());
        gunInfoModle.setTypeName(wsBean.getTypeName());
        gunInfoModle.setGunId(wsBean.getGunId());
        gunInfoModle.setTypeId(wsBean.getTypeId());
        gunInfoModle.setCuPrice(wsBean.getCuPrice());

        gunInfoModleDao.update(gunInfoModle);
    }

    public void deletetByGunId(String gunId){
        GunInfoModle gun = gunInfoModleDao.queryBuilder().where(GunInfoModleDao.Properties.GunId.eq(gunId)).unique();
        gunInfoModleDao.delete(gun);
    }

    public void deleteAll() {
        gunInfoModleDao.deleteAll();
    }

    /**
     * 更新油枪的 油品价格
     * @param portNum
     * @param price
     */
    public void updateGunOilTypePrice(String portNum,String price){

    }
}
